local wezterm = require 'wezterm'

-- local launch_menu = {}
-- local default_prog = {}
-- local set_environment_variables = {}

-- Shell
-- if wezterm.target_triple == 'x86_64-pc-windows-msvc' then
--     table.insert( launch_menu, {
--         label = 'PowerShell',
--         args = { 'powershell.exe', '-NoLogo' }
--      } )
--     table.insert( launch_menu, {
--         label = "WSL",
--         args = { "wsl.exe", "--cd", "/home/" }
--      } )
--     default_prog = { 'powershell.exe', '-NoLogo' }
-- elseif wezterm.target_triple == 'x86_64-unknown-linux-gnu' then
--     table.insert( launch_menu, {
--         label = 'Bash',
--         args = { 'bash', '-l' }
-- --         args = { '/usr/bin/fish'}
--      } )
--     default_prog = { 'bash', '-l' }
-- else
--     table.insert( launch_menu, {
--         label = 'Zsh',
--         args = { 'zsh', '-l' }
--      } )
--     default_prog = { 'zsh', '-l' }

--     table.insert( launch_menu, {
--         label = 'Fish',
--         args = { 'fish', '-l' }
--      } )
--     default_prog = { 'fish', '-l' }
-- end
-- wezterm.default_prog = { 'bash', '-l' }
-- Title

-- table.insert( launch_menu, {
--     label = 'Fish',
--     args = { '/usr/bin/fish'}
--     } )
-- default_prog = { 'fish'}


-- config.set_environment_variables = set_environment_variables,



-- Startup
wezterm.on( 'gui-startup', function( cmd )
    local tab, pane, window = wezterm.mux.spawn_window( cmd or {} )
    window:gui_window():maximize()
end )

local config = {
    -- Basic
    check_for_updates = false,
    switch_to_last_active_tab_when_closing_tab = false,
    enable_scroll_bar = true,

    -- Window
    native_macos_fullscreen_mode = true,
    adjust_window_size_when_changing_font_size = true,
    window_background_opacity = 0.9,
    window_padding = {
        left = 5,
        right = 5,
        top = 5,
        bottom = 5
     },
    window_background_image_hsb = {
        brightness = 0.8,
        hue = 1.0,
        saturation = 1.0
     },

    -- Font
    font = wezterm.font_with_fallback { 'FiraCode Nerd Font' },
    font_size = 12,
--     freetype_load_target = "Mono",

    -- Tab bar


    -- Keys
    disable_default_key_bindings = false,
    leader = {
        key = 'b',
        mods = 'CTRL'
     },

    -- Allow using ^ with single key press.
    use_dead_keys = false,

    keys = {  -- New/close pane
 -- Pane navigation
    {
        key = 'LeftArrow',
        mods = 'ALT',
        action = wezterm.action {
            ActivatePaneDirection = 'Left'
         }
     }, {
        key = 'DownArrow',
        mods = 'ALT',
        action = wezterm.action {
            ActivatePaneDirection = 'Down'
         }
     }, {
        key = 'UpArrow',
        mods = 'ALT',
        action = wezterm.action {
            ActivatePaneDirection = 'Up'
         }
     }, {
        key = 'RightArrow',
        mods = 'ALT',
        action = wezterm.action {
            ActivatePaneDirection = 'Right'
         }
     },
     -- Tab navigation
     {
        key = 'RightArrow',
        mods = 'CTRL|ALT',
        action = wezterm.action.MoveTabRelative(1)
     },
    {
        key = 'LeftArrow',
        mods = 'CTRL|ALT',
        action = wezterm.action.MoveTabRelative(-1)
     },
    {
        key = 'z',
        mods = 'ALT',
        action = 'TogglePaneZoomState'
     },{
        key = 't',
        mods = 'CTRL',
        action = wezterm.action.SpawnTab 'CurrentPaneDomain'
     }, {
        key = 't',
        mods = 'CTRL|SHIFT',
        action = wezterm.action.SpawnTab 'DefaultDomain'
     }, {
        key = 'd',
        mods = 'CTRL|SHIFT',
        action = wezterm.action.DetachDomain 'CurrentPaneDomain'
     }, {
        key = 'w',
        mods = 'CTRL|SHIFT',
        action = wezterm.action.CloseCurrentPane {
            confirm = true
         }
     },
     -- Resize
    {
        key = 'LeftArrow',
        mods = 'ALT|SHIFT',
        action = wezterm.action {
            AdjustPaneSize = { 'Left', 5 }
         }
     }, {
        key = 'DownArrow',
        mods = 'ALT|SHIFT',
        action = wezterm.action {
            AdjustPaneSize = { 'Down', 5 }
         }
     }, {
        key = 'UpArrow',
        mods = 'ALT|SHIFT',
        action = wezterm.action {
            AdjustPaneSize = { 'Up', 5 }
         }
     }, {
        key = 'RightArrow',
        mods = 'ALT|SHIFT',
        action = wezterm.action {
            AdjustPaneSize = { 'Right', 5 }
         }
     },
     -- Split
    {
        key = '\\',
        mods = 'CTRL',
        action = wezterm.action.SplitVertical {
            domain = 'CurrentPaneDomain'
         }
     }, {
        key = '/',
        mods = 'CTRL',
        action = wezterm.action.SplitHorizontal {
            domain = 'CurrentPaneDomain'
         }
     }, -- Copy/paste buffer
    {
        key = '[',
        mods = 'LEADER',
        action = 'ActivateCopyMode'
     }, {
        key = ']',
        mods = 'LEADER',
        action = 'QuickSelect'
     } },

    color_scheme = 'Monokai (base16)',

    inactive_pane_hsb = {
        hue = 1.0,
        saturation = 1.0,
        brightness = 1.0
     },

    mouse_bindings = {  -- Paste on right-click
    {
        event = {
            Down = {
                streak = 1,
                button = 'Right'
             }
         },
        mods = 'NONE',
        action = wezterm.action {
            PasteFrom = 'Clipboard'
         }
     }, -- Change the default click behavior so that it only selects
    -- text and doesn't open hyperlinks
    {
        event = {
            Up = {
                streak = 1,
                button = 'Left'
             }
         },
        mods = 'NONE',
        action = wezterm.action {
            CompleteSelection = 'PrimarySelection'
         }
     }, -- CTRL-Click open hyperlinks
    {
        event = {
            Up = {
                streak = 1,
                button = 'Left'
             }
         },
        mods = 'CTRL',
        action = 'OpenLinkAtMouseCursor'
     } },


 }

-- local launch_menu = {}
-- table.insert( launch_menu, {
--     label = 'Bash',
--     args = { 'bash', '-l' }
--     } )
-- default_prog = { 'bash', '-l' }

-- config.default_prog = default_prog
-- config.launch_menu = launch_menu

config.ssh_domains = {
  {
    name = '2080',
    username = 'isidore',
    remote_address = '49.52.10.60',
--     multiplexing = 'None'
--     remote_wezterm_path = "/home/isidore/.cargo/bin/wezterm",
--     ssh_option = {
--       identityfile = '~/.ssh/id_rsa.pub',
--     },
    },
  {
    name = 'seven',
    username = 'fwang',
    remote_address = '59.78.189.141:2323',
--     multiplexing = 'None',
--     remote_wezterm_path = "$HOME/App/bin/wezterm",
--     ssh_option = {
--       identityfile = '~/.ssh/id_rsa.pub',
--     },
    },
    {
        name = '3090',
        username = 'isidore',
        remote_address = '49.52.10.39',
    --     multiplexing = 'None',
    --     remote_wezterm_path = "$HOME/App/bin/wezterm",
    --     ssh_option = {
    --       identityfile = '~/.ssh/id_rsa.pub',
    --     },
        },
}

config.unix_domains = {
    {name = 'unix'},
}
config.default_gui_startup_args = { 'connect', 'unix' }


config.enable_tab_bar = true
config.hide_tab_bar_if_only_one_tab = false
config.show_tab_index_in_tab_bar = false
config.tab_bar_at_bottom = true
config.tab_max_width = 55
config.use_fancy_tab_bar = true
-- The filled in variant of the < symbol
local SOLID_LEFT_ARROW = wezterm.nerdfonts.pl_right_hard_divider
-- The filled in variant of the > symbol
local SOLID_RIGHT_ARROW = wezterm.nerdfonts.pl_left_hard_divider
-- This function returns the suggested title for a tab.
-- It prefers the title that was set via `tab:set_title()`
-- or `wezterm cli set-tab-title`, but falls back to the
-- title of the active pane in that tab.

function basename( s )
    return string.gsub( s, '(.*[/\\])(.*)', '%2' )
end

wezterm.on( 'format-tab-title',
    function( tab, tabs, panes, config, hover, max_width )
    local edge_background = '#0b0022'
    local background = '#1b1032'
    local foreground = '#808080'

    if tab.is_active then
      background = '#2b2042'
      foreground = '#c0c0c0'
    elseif hover then
      background = '#3b3052'
      foreground = '#909090'
    end
    local edge_foreground = background
    local pane = tab.active_pane
    local title = pane.title
    if pane.domain_name then
        title = pane.domain_name .. ':' .. title
    end
    return {
      { Background = { Color = edge_background } },
      { Foreground = { Color = edge_foreground } },
      { Text = SOLID_LEFT_ARROW },
      { Background = { Color = background } },
      { Foreground = { Color = foreground } },
      { Text = title },
      { Background = { Color = edge_background } },
      { Foreground = { Color = edge_foreground } },
      { Text = SOLID_RIGHT_ARROW },
    }
    end
)

return config
